﻿using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace AppDataStruct
{
    public static class Data2Xml
    {
        public static bool Class2Xml(object obj, out string xmlString)
        {
            if (obj == null)
            {
                xmlString = "参数为空";
                return false;
            }

            try
            {
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                StringBuilder sb = new StringBuilder();
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = false;
                settings.Encoding = Encoding.UTF8;
                using (XmlWriter writer = XmlWriter.Create(sb, settings))
                {
                    //去除默认命名空间xmlns:xsd和xmlns:xsi
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    serializer.Serialize(writer, obj, ns);
                }
                xmlString = sb.ToString();
                return true;
            }
            catch (Exception ex)
            {
                xmlString = ex.Message;
                Log.WriteException(ex.ToString());
                Log.WriteAsync("Class2Xml出错:" + ex.Message);
                return false;
            }
        }

        public static object XMLFile2Class(string filePath, object obj, out string msg)
        {
            if (filePath == "")
            {
                msg = "XML文件路径为空";
                return null;
            }
            if (!File.Exists(filePath))
            {
                msg = "XML文件不存在";
                return null;
            }
            if (obj == null)
            {
                msg = "传入的OBJ对象为空";
                return null;
            }
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            object result = null;
            try
            {
                using (Stream fStream = File.OpenRead(filePath))
                {
                    result = serializer.Deserialize(fStream);
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                Log.WriteException(ex.ToString());
                Log.WriteAsync("XMLFile2Class出错:" + ex.Message);
            }
            msg = "";
            return result;
        }

        public static bool Class2File(object obj, string FilePath, out string msg)
        {
            string strXml = "";
            if (!Class2Xml(obj, out strXml))
            {
                msg = strXml;
                return false;
            }

            if (FilePath == "")
            {
                msg = "文件路径为空";
                return false;
            }

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(strXml);
                doc.Save(FilePath);
                msg = "";
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                Log.WriteException(ex.ToString());
                Log.WriteAsync("Class2File出错:" + msg);
                return false;
            }

        }

        public static string XmlDicReader(string path, string code)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                string content = File.ReadAllText(path, Encoding.UTF8);
                doc.LoadXml(content);
                XmlNodeList list = doc.SelectSingleNode("ROWDATA").ChildNodes;
                string result = "";
                foreach (XmlNode node in list)
                {
                    if (node.SelectSingleNode("CODE").InnerText == code)
                    {
                        result = node.SelectSingleNode("CONTENT").InnerText;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                Log.WriteAsync("查询XML字典时出错" + ex.Message);
                Log.WriteException(ex.ToString());
                return "";
            }
        }
    }
}
